<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * 选择或者添加一个相册对应到相应的用户.
 * 完成跳转到上传图片.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2)  Adjusts.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements
require_once INSTALLDIR . '/lib/slh_util.php';
require_once INSTALLDIR . '/lib/slh_rights.php';

require_once INSTALLDIR . '/widgets/slh_form_addphotoset.php';
require_once INSTALLDIR . '/widgets/slh_form_selectphotoset.php';

require_once INSTALLDIR . '/classes/PhotoSet.php';

class ChoosephotosetAction extends Action
{
	/**
	 * The photoset array relate to corresponding user or type.
	 */
	var $photosetarray = array();
	
	/**
	 * Has some instruction?
	 */
	var $instruction = null;
	
	function title()
	{
		return '选择已有相册或新建相册!';
	}
	
	function prepare($args)
	{
		if ( !Rights::hasRight(slh_get_userrights(), RIGHT_PHOTOSET)) {
			return false;
		}
		
		parent::prepare($args);
		
		// Get the user id or the type of the this photo set to go.
		$user_id = 0;
		
		// Get the existed photosets.
		$photoset = new PhotoSet();
		$photoset->user_id = $user_id;
		if ($photoset->find()) {
			while ($photoset->fetch()) {
				$this->photosetarray[$photoset->id] = $photoset->title;
			}
		}
		
		return true;
	}
	
	function handle($args)
	{
		parent::handle($args);
		
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			// CSRF protection and Refreshing-post protection.
			$this->checkSessionToken();
		
			$formtoken = $this->getTrimmedArg('formtoken');
			if ( !empty($formtoken)) {
				if ('selectphotoset' == $formtoken) {
					$this->select();
				} else if ('addphotoset' == $formtoken) {
					$this->tryAdd();
				}
			}
		} else {
			$this->showAction();
		}
	}
	
	/**
	 * Overrideing the showInnerContent().
	 */
	function showInnerContent()
	{
		$this->element('h3', null, '选择/添加相册');
		
		$this->instruction('重已有相册中选中并添加图片,或者新建一个相册后添加图片.');
		
		$this->element('hr');
		
		// Show the instruction.
		$this->showInstruction();
		
		if ( !empty($this->photosetarray)) {
			$selectForm = new SelectphotosetForm($this, $this->photosetarray);
			$selectForm->show();
		}
		
		$addForm = new AddphotosetForm($this, $this->args);
		$addForm->show();
		
		$this->element('hr');
	}
	
// Overrided ends //////////////////////////////////////////////////////////////
	/**
	 * Shows the form with instructions.
	 */
	function showAction($instruction = null)
	{
		$this->instruction = $instruction;
		$this->showPage();
	}
	
	/**
	 * Shows the instruction this page.
	 */
	function showInstruction()
	{
		if ($this->instruction) {
			$this->elementStart('div', array('class' => 'cls_div_instruction'));
			$this->instruction($this->instruction);
			$this->elementEnd('div');
		}
	}
	
	/**
	 * Try to add the new photo set into the database.
	 */
	function tryAdd()
	{
// TODO: Getting the user_id by adding, 
// 0 for xmuslh, others for association?
		$user_id	= 0;
		$title		= $this->getTrimmedArg('title');
		$message	= $this->getTrimmedArg('message');
		
		if (empty($title)) {
			$this->showAction('请输入相册名称.');
		} else {
			// Adds into the database.
			$photoset_id = PhotoSet::add(array('user_id' => $user_id,
											   'title' => $title,
											   'message' => $message));
			
			if ( !$photoset_id) {
				$this->showAction('添加相册失败!');
				return;
			}
			
			// Success.
			// Unset the seesion token preventing post-again.
			$this->unsetSessionToken();
			
			// Unset the seted values.
			unset($this->args['title']);
			unset($this->args['message']);
			
			slh_redirect(slh_action2url('addphoto', array('gphotoset_id' => $photoset_id)), 303);
		}
	}
	
	/**
	 * Select a before created photo set.
	 */
	function select()
	{
		$photoset_id = $this->getTrimmedArg('photoset_id');		
		slh_redirect(slh_action2url('addphoto', array('gphotoset_id' => $photoset_id)), 303);
	}
}

?>